package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/9 9:58
 */
public class 叶子相似的树872 {

    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> n1 = new ArrayList<>();
        recur(root1, n1);
        List<Integer> n2 = new ArrayList<>();
        recur(root2, n2);
        if (n1.size() != n2.size()) return false;
        for (int i = 0; i < n1.size(); i++) {
            if (!n1.get(i).equals(n2.get(i))) return false;
        }
        return true;
    }

    public void recur(TreeNode node, List<Integer> res) {
        if (node == null) {
            return;
        }
        recur(node.left, res);
        if (node.left == null && node.right == null) {
            res.add(node.val);
        }
        recur(node.right, res);
    }
}
